/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.gzchengguo.cn
* 注意：
* 本软件为www.gzchengguo.cn开发研制，未经购买不得商用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package link.chengguo.orangemall.modules.user.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import link.chengguo.orangemall.api.ApiResult;
import link.chengguo.orangemall.common.bean.LocalUser;
import link.chengguo.orangemall.common.interceptor.AuthCheck;
import link.chengguo.orangemall.dozer.service.IGenerator;
import link.chengguo.orangemall.modules.domain.OrangeStoreProductBrowse;
import link.chengguo.orangemall.modules.product.service.OrangeStoreProductService;
import link.chengguo.orangemall.modules.service.OrangeStoreProductBrowseService;
import link.chengguo.orangemall.modules.service.dto.OrangeStoreProductBrowseDto;
import link.chengguo.orangemall.modules.service.dto.OrangeStoreProductBrowseQueryCriteria;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @author chengguo
* @date 2020-08-18
*/
@AllArgsConstructor
@Api(tags = "浏览记录管理")
@RestController
@RequestMapping("/userProductBrowse")
public class UserProductBrowseController {

    private final OrangeStoreProductBrowseService orangeStoreProductBrowseService;
    private final OrangeStoreProductService storeProductService;
    private IGenerator generator;

    @AuthCheck
    @GetMapping
    @ApiOperation("查询浏览记录")
    public ApiResult getOrangeStoreProductBrowses(OrangeStoreProductBrowseQueryCriteria criteria, Pageable pageable){
        criteria.setUid(LocalUser.getUser().getUid().intValue());
        List<OrangeStoreProductBrowseDto> list = generator.convert((List) orangeStoreProductBrowseService.queryAll(criteria, pageable).get("content"), OrangeStoreProductBrowseDto.class);
        for (OrangeStoreProductBrowseDto browseDto : list) {
            browseDto.setProduct(storeProductService.getById(browseDto.getProductId()));
        }
        return ApiResult.ok(list);
    }


    @AuthCheck
    @ApiOperation("删除浏览记录")
    @PostMapping("/delete/{id}")
    public ApiResult deleteAll(@PathVariable Integer id) {
        OrangeStoreProductBrowse browse = orangeStoreProductBrowseService.getById(id);
        if(browse == null || !browse.getUid().equals(LocalUser.getUser().getUid())){
            return ApiResult.fail();
        }
        orangeStoreProductBrowseService.removeById(id);
        return ApiResult.ok();
    }
}
